package com.qf.fmall2202.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.fmall2202.index.entity.Category;
import com.qf.fmall2202.index.mapper.CategoryMapper;
import com.qf.fmall2202.index.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.fmall2202.product.entity.Product;
import com.qf.fmall2202.product.entity.ProductImg;
import com.qf.fmall2202.product.service.IProductImgService;
import com.qf.fmall2202.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author lzh
 * @since 2022-11-07
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Autowired
    IProductService productService;

    @Autowired
    IProductImgService productImgService;

    @Transactional
    @Override
    public List<Category> categoryList() {

        //1.查询出所有菜单信息
        List<Category> allCategories = this.list();

        //2.组织分类  商品信息为层级结构
        List<Category> topLevelCategory = organizeCatory(0,allCategories);

        return topLevelCategory;


    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<Category> categoryRecommends() {

        //1.查询顶级分类信息
        List<Category> topCategoryList = this.list(new QueryWrapper<Category>().eq("parent_id", 0));


        //2.查询各个顶级分类下的商品信息
        topCategoryList.forEach(category -> {
            //1.插叙该分类下的商品按照销量排名前6的商品
            Page<Product> productPage = new Page<>(1,6);
            productPage.addOrder(OrderItem.desc("sold_num"));
            LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<Product>().eq(Product::getRootCategoryId, category.getCategoryId());


            productService.page(productPage,queryWrapper);


            //把查询到的商品信息挂载到Category的对象上
            List<Product> productList = productPage.getRecords();

            //判断当前如果顶级分类下真的有商品时，遍历商品填充图片信息
            if (productList != null && productList.size()>0){
                //把商品的图片信息查询出来
                final QueryWrapper<ProductImg> productImgQueryWrapper = new QueryWrapper<>();

                // List<Product> --> List<ProductID>
                final List<String> productIdList = productList.stream().map(Product::getProductId).collect(Collectors.toList());

                productImgQueryWrapper.in("item_id",productIdList);

                final List<ProductImg> productImgsList = productImgService.list(productImgQueryWrapper);


                // 分组 List<ProductImg>  ---> Map<商品ID， List<商品图片> >
                final Map<String, List<ProductImg>> productImgMap = productImgsList.stream()
                        .collect(Collectors.groupingBy(ProductImg::getItemId));

                // 填充 productList
                productList.forEach(product -> {

                    List<ProductImg> productImgs = productImgMap.get(product.getProductId());
                    if (productImgs == null){
                        productImgs = new ArrayList<>();
                    }
                    product.setImgs(productImgs);
                });
            }

            category.setProducts(productList);
        });

        return topCategoryList;


    }

    /**
     * 按照给定的 parentId组织成分类信心的层级结构
     * @param
     * @param allCategories
     * @return
     */
    private List<Category> organizeCatory(int parentId, List<Category> allCategories) {

        //0.按照 parentId 对 allCategories 进行分组
        Map<Integer, List<Category>> parentIdMap = allCategories.stream()
                .collect(Collectors.groupingBy(Category::getParentId));

//        //1.查询出所有的顶级分类信息得到一个list<顶级分类>
//        List<Category> topCategoryList = allCategories
//                .stream()
//                .filter(category -> category.getParentId().equals(parentId))
//                .collect(Collectors.toList());

        List<Category> topCategoryList = parentIdMap.get(parentId);

        //2.遍历所有的顶级分类  为每一个顶级分类  挂载到子类
        populateChildCategory(topCategoryList,parentIdMap);


        return topCategoryList;
    }

    private void populateChildCategory(List<Category> parentCategory, Map<Integer, List<Category>> parentIdMap) {


        parentCategory.forEach(category -> {
            //1.寻找当前分类的下级分类
            List<Category> childCategories = parentIdMap.get(category.getCategoryId());

            if (childCategories != null && childCategories.size()>0){
                populateChildCategory(childCategories,parentIdMap);
            }

            //挂载
            category.setCategories(childCategories);
        });

    }



    /**
     * 为父分类填充子信息分类
     * @param parentCategory
     * @param allCategories
     */
    private void populateChildCategory(List<Category> parentCategory, List<Category> allCategories) {

        //1.遍历  parentCategory  ，为每一个 分类信息查找子分类并挂载
        parentCategory.forEach(category ->
        {
            //1.查找子分类信息
            List<Category> childCategoryList = allCategories.stream()
                    .filter(c -> c.getParentId().equals(category.getCategoryId()))
                    .collect(Collectors.toList());

            //判断，如果孩子不等于null，并且size大于0
            if (  childCategoryList.size()>0  ){
                populateChildCategory(childCategoryList,allCategories);
            }
            //2.挂载
            category.setCategories(childCategoryList);
        });


    }
}
